package com.zck.aiagent.tools;

import cn.hutool.core.io.FileUtil;
import com.zck.aiagent.constant.FileConstant;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

@Component
public class FileOperationTool {

    private final String FILE_DIR= FileConstant.FILE_SAVE_DIR+"\\file";

    /**
     * 读取文件
     * @param fileName 文化名称
     * @return
     */
    @Tool(description = "Read content from a file")
    public String readFile(@ToolParam(description = "Name of the file to read") String fileName) {
        //1.构建文件路径
        String filePath=FILE_DIR+"\\"+fileName;
        try {
            //2.读取文化
            return FileUtil.readUtf8String(filePath);
        }catch (Exception e){
            return "Error reading file: "+e.getMessage();
        }
    }

    @Tool(description = "Write content to a file")
    public String writeFile(@ToolParam(description = "Name of the file to write")String fileName,
                            @ToolParam(description = "Content to write to the file")String content) {
        String filePath=FILE_DIR+"\\"+fileName;
        try {
            FileUtil.mkdir(FILE_DIR);
            FileUtil.writeUtf8String(content,filePath);
            return "File written successfully to: "+filePath;
        }catch (Exception e){
            return "Error writing file: "+e.getMessage();
        }
    }
}
